package com.llf.Blog.Controller;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llf.Blog.Mapper.TypeMapper;
import com.llf.Blog.Pojo.Blog;
import com.llf.Blog.Pojo.Type;
import com.llf.Blog.Service.BlogService;
import com.llf.Blog.Service.TypeService;
import com.llf.Blog.ServiceImpl.BlogServiceImpl;
import com.llf.Blog.ServiceImpl.TypeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;



import java.util.List;

/**
 * @author machenike
 */
@Controller
@Api(value = "提供获取分类信息接口",tags = "博客分类管理")
public class TypesController {


    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;

    /**
     *  通过点击type类型完成搜索并返回结果,首次进入默认TypeId为1
     * @param model
     * @param pagNum 当前页
     * @param id TypeId
     * @return
     */
    @ApiOperation("根据博客类型搜索接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pageNum", value = "当前页",required = true),
            @ApiImplicitParam(dataType = "long",name = "id", value = "类型id",required = true)
    })
    @GetMapping("/toTypes/{id}")
    public String toTypesByTapId(Model model,  Integer pagNum, @PathVariable("id") Long id){
        if (pagNum==null){
            pagNum=1;
        }

        PageHelper.startPage(pagNum,4);
        List<Blog> blogList = blogService.getBlog(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        PageHelper.clearPage();
        List<Type> typeList = typeService.getCountType();
        long n=id;
        model.addAttribute("typeList",typeList);
        model.addAttribute("page",pageInfo);
        model.addAttribute("n",id);
        return "types";
    }


    /**
     *  前往后台分类管理页 默认为第一个
     * @param model  传递数据
     * @param current 当前页
     * @param pageSize 每页大小
     * @return
     */
    @ApiOperation("后台分类管理页跳转")
    @GetMapping("/admin/toTypes/{current}")
    public String toAdminTypes( Model model,@PathVariable("current") Integer current, Integer pageSize){
        BaseMapper<Type>  mapper = typeService.getBaseMapper();
        pageSize=4;
        Page<Type> page = new Page<>(current, 6);
        Page<Type> selectPage = mapper.selectPage(page, null);
        model.addAttribute("page",selectPage);
        return "admin/types";
    }

    /**
     *  跳转到分类添加页
     * @return
     */
    @ApiOperation("后台分类添加页跳转接口")
    @GetMapping("/admin/toType-add")
    public String toAdminTypeAdd(){
        return  "admin/type-add";
    }


    /**
     *  新增一个分类
     * @param type 一个类型对象
     * @param attributes 信息传递
     * @param name  通过分类名字 查询 该分类是否存在
     * @param model 传递数据
     * @return
     */
    @ApiOperation("博客分类新增接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "name", value = "新增分类名字",required = true)
    })
    @PostMapping("/insertType")
    public String insertType(@RequestBody Type type, RedirectAttributes attributes,String name,Model model){
        Type type1 = typeService.getByName(name);
        if (type1 != null) {
            model.addAttribute("error","该分类已存在");
            return "admin/type-add";
        }else {
            typeService.insertType(type);
            //使用它 可以给前端传递一个提示框
            attributes.addFlashAttribute("message","添加成功");
            return "redirect:/admin/toTypes/1";
        }
        }


    /**
     *  跳转到后台分类更新页
     * @param id 通过Id查询到该分类 并将该分类对象传递到更新页
     * @param model
     * @return
     */
    @ApiOperation("后台分类更新页跳转接口")
    @GetMapping("/toUpdateType/{id}")
      public String toUpdate( @PathVariable("id") Long id,Model model){
           Type type = typeService.getById(id);
           model.addAttribute("type",type);
           return "admin/type-update";
       }


    /**
     * 更新一个分类
     * @param type 更新好的分类
     * @param model
     * @param attributes  信息传递
     * @param oldName 修改之前的名字
     * @return
     */
    @ApiOperation("分类更新接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "oldName", value = "修改之前的名字",required = true)

    })
    @PostMapping("/updateByType")
     public String toUpdate( Type type, Model model,RedirectAttributes attributes,String oldName){
          Type type1 = typeService.getByName(type.getName());
          if (type1 != null) {
              model.addAttribute("error","该分类已存在");
              return "admin/type-update";
          }else {
              typeService.update(type);
              attributes.addFlashAttribute("message","名称===>"+oldName+"<===修改成功为===>"+type.getName());
              return "redirect:/admin/toTypes/1";
          }
    }


    /**
     * 通过ID删除一个分类
     * @param id
     * @param attributes
     * @return
     */
    @ApiOperation("分类删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long",name = "id", value = "类型id",required = true)
    })
    @GetMapping("/deleteType/{id}")
    public String deleteType( @PathVariable("id") Long id,RedirectAttributes attributes){
         typeService.deleteById(id);
        attributes.addFlashAttribute("message","删除成功");
         return "redirect:/admin/toTypes/1";
    }
}
